﻿using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using PropertyManagementSystemAPI.Models;

namespace PropertyManagementSystemAPI.Controllers
{
    /// <summary>
    /// 小区资产管理
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]

    [ApiExplorerSettings(GroupName = "物业")]
    public class AssetsController : ControllerBase
    {
        public readonly IFreeSql db;// 这里声明了一个名为db的只读字段，其类型为IFreeSql接口，用于表示一个FreeSql数据库连接实例
        public AssetsController(IFreeSql _db)//这是AssetsController类的构造函数，接受一个IFreeSql类型的参数_db，并将其赋值给之前声明的db字段
        {
            this.db = _db;
        }
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="ys">32</param>
        /// <param name="ym"></param>
        /// <param name="name"></param>
        /// <returns></returns>

        [HttpGet]
        public object Get(int ys, int ym, string? name)
        {
            int pageSize = ys; // 每页显示的条目数
            int pageIndex = ym; // 当前页码
            var query = db.Select<CellAssets>().ToList();
            int totalCount = query.Count(); // 总条目数
            var list = query.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();

            // 获取表中所有列的数据
            var data = list.Select(item => new
            {
                item.CAId,
                item.CABrand,
                item.CABuyTime,
                item.CACellId,
                item.CACount,
                item.CACreateTime,
                item.CAName,
                item.CAPrice,
                item.CAUserYear
            }).ToList();

            return new
            {
                TotalCount = totalCount,
                Data = data
            };
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="ww"></param>
        /// <returns></returns>
        [HttpPost]
        public int Post(CellAssets ww)
        {
            var studentInfos = new CellAssets()
            {
                CACellId = ww.CACellId,
                CAName = ww.CAName,
                CABrand = ww.CABrand,
                CAPrice = ww.CAPrice,
                CACount = ww.CACount,
                CAUserYear = ww.CAUserYear,
                CABuyTime = ww.CABuyTime,
                CACreateTime = ww.CACreateTime,
            };
            var q = db.Insert(studentInfos).ExecuteAffrows();
            return q;
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="id"></param>
        /// <param name="ww"></param>
        /// <returns></returns>
        [HttpPut]
        public int Put(int id, CellAssets ww)
        {
            var mm = db.Update<CellAssets>().Set(a => new CellAssets
            {
                CAId = ww.CAId,
                CACellId = ww.CACellId,
                CAName = ww.CAName,
                CABrand = ww.CABrand,
                CAPrice = ww.CAPrice,
                CACount = ww.CACount,
                CAUserYear = ww.CAUserYear,
                CABuyTime = ww.CABuyTime,
                CACreateTime = ww.CACreateTime,
            }).Where(a => a.CAId == id).ExecuteAffrows();
            return mm;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        [HttpDelete]
        public int Delete(int id)
        {
            var affectedRows = db.Delete<CellAssets>()
                .Where(a => a.CAId == id)
                .ExecuteAffrows(); //id删除
            return affectedRows;
        }


    }
}

